package com.xinchen.aiowaste.vm

import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.GsonUtils
import com.maple.ble.utils.BleLogger
import com.maple.model.data.AioUserInfo
import com.maple.model.entity.aio.LoginEntity
import com.maple.model.entity.aio.WasteListEntity
import com.maple.utils.store.AioDBHelper
import com.maple.utils.util.HttpRequestUtils
import com.maple.utils.util.LogUtils
import com.xinchen.aiowaste.base.BaseViewModel
import com.xinchen.aiowaste.model.repository.CommonRepository
import java.util.WeakHashMap

class WasteListViewModel: BaseViewModel() {

    private val repository by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { CommonRepository() }


    val userInfo: AioUserInfo? by lazy {
        AioDBHelper.getUser()
    }

    val entId: String by lazy {
        userInfo?.entId?:""
    }

    val wasteData: MutableLiveData<WasteListEntity.Data> = MutableLiveData()

    fun getWasteList(params: WeakHashMap<String, Any>,isFirst: Boolean? = false) {
        launchOnlyResult(block = {
            repository.wasteList(params)
        }, success = {
            val entity = GsonUtils.fromJson(GsonUtils.toJson(it), WasteListEntity::class.java)
            entity.data?.let { data ->
                wasteData.postValue(data)
            }?: let {
               if(isFirst == true) {
                   defUI.onEmpty()
               }
            }
        }, error = {
            if(isFirst == true) {
                defUI.onEmpty()
            }
            LogUtils.logGGQ("请求异常：${it.errMsg}")
        })
    }
}